package org.thss.petri.gef.commands;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import org.thss.petri.gef.figures.PlaceFigure;
import org.thss.petri.gef.figures.TransitionFigure;
import org.thss.petri.model.Node;
import org.thss.petri.model.NodeConstraint;
import org.thss.petri.model.PetriFactory;
import org.thss.petri.model.PetriNet;
import org.thss.petri.model.Place;
import org.thss.petri.model.Transition;

public class CreateNodeCommand extends Command {
	
	private PetriNet net;
	private Node node;
	private Point position;
	private Dimension size;

	public PetriNet getNet() {
		return net;
	}

	public void setNet(PetriNet net) {
		this.net = net;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}
	
	@Override
	public boolean canExecute() {
		return net != null && node != null && position != null;
	}

	@Override
	public void execute() {
		NodeConstraint constraint = PetriFactory.eINSTANCE.createNodeConstraint();
		constraint.setX(position.x);
		constraint.setY(position.y);
		if (size != null) {
			constraint.setWidth(size.width);
			constraint.setHeight(size.height);
		}
		else if (node instanceof Place) {
			constraint.setWidth(PlaceFigure.DEFAULT_WIDTH);
			constraint.setHeight(PlaceFigure.DEFAULT_HEIGHT);
		}
		else if (node instanceof Transition) {
			constraint.setWidth(TransitionFigure.DEFAULT_WIDTH);
			constraint.setHeight(TransitionFigure.DEFAULT_HEIGHT);
		}
		node.setConstraint(constraint);
		node.setNet(net);
		net.getNodes().add(node);
		super.execute();
	}

	@Override
	public void redo() {
		net.getNodes().add(node);
	}

	@Override
	public void undo() {
		net.getNodes().remove(node);
	}

	public Point getPosition() {
		return position;
	}

	public void setPosition(Point position) {
		this.position = position;
	}

	public Dimension getSize() {
		return size;
	}

	public void setSize(Dimension size) {
		this.size = size;
	}

}
